//
//  HHChatHelper.m
//  HHChatKit
//
//  Created by Henry on 2021/5/10.
//

#import "HHChatHelper.h"
#import <HHTool/HHTool.h>
#import "HHChatConfiguration.h"
#import "HHResources.h"

@implementation HHChatHelper

+ (BOOL)isIpad {
    return [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad;
}

+ (UIWindow *)keyWindow {
    if (@available(iOS 13.0, *)) {
        UIWindow *foundWindow = nil;
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for (UIWindow *window in windows) {
            if (window.isKeyWindow) {
                foundWindow = window;
                break;
            }
        }
        if (foundWindow == nil) {
            if (windows.count > 0) {
                foundWindow = windows[0];
            }
        }
        return foundWindow;
    }
    return [UIApplication sharedApplication].keyWindow;
}

+ (BOOL)isPortrait {
    UIInterfaceOrientation orientation;
    if (@available(iOS 13.0, *)) {
        orientation = [self keyWindow].windowScene.interfaceOrientation;
    } else {
        orientation = [UIApplication sharedApplication].statusBarOrientation;
    }
    return UIInterfaceOrientationIsPortrait(orientation);
}

+ (CGFloat)statusBarHeight {
    return [[UIApplication sharedApplication] statusBarFrame].size.height;
}

+ (CGFloat)navBarHeight {
    return [UIWindow topViewController].navigationController.navigationBar.size.height;
}

+ (CGFloat)statusNavHeight {
    return [self statusBarHeight] + [UIWindow topViewController].navigationController.navigationBar.size.height;
}

+ (CGFloat)topSafeHeight {
    return [self safeAreaInset].top;
}

+ (CGFloat)bottomSafeHeight {
    return [self safeAreaInset].bottom;
}

+ (UIEdgeInsets)safeAreaInset {
    if (@available(iOS 11.0, *)) {
        return [self keyWindow].safeAreaInsets;
    } else {
        return UIEdgeInsetsMake([self statusBarHeight], 0, 0, 0);
    }
}


#pragma mark --------

+ (NSString *)UUID {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] > 6.0) {
       return  [[NSUUID UUID] UUIDString];
    } else {
        CFUUIDRef uuidRef = CFUUIDCreate(NULL);
        CFStringRef uuid = CFUUIDCreateString(NULL, uuidRef);
        CFRelease(uuidRef);
        return (__bridge_transfer NSString *)uuid;
    }
}

+ (NSString *)generateUUID {
    NSString *uuid = [self UUID];
     return uuid.hh_md5String;
}

+ (CGSize)getThumbSizeFromOrigin:(CGSize)size {
    CGFloat heightMax = MIN([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) * 0.4;
    if (size.height > size.width) {
        size.width = size.width / size.height * heightMax;
        size.height = heightMax;
    } else {
        size.height = size.height / size.width * heightMax;
        size.width = heightMax;
    }
    return size;
}

+ (NSString *)randomAvatarUrl {
    return [NSString stringWithFormat:@"https://picsum.photos/id/%d/200/200", rand()%999];
}

+ (UIImage *)defaultHead {
    return [HHResources getImageFromMessage:@"default_head"];
}

#pragma mark - 本地数据

+ (NSString *)baseDir {
    NSString *loginId = [HHChatConfiguration shareInstance].loginId;
    return [NSString stringWithFormat:@"%@/%@/%@", [HHPathTool getDocumentPath], @"com_henry_chat_data", [loginId hh_md5String]];
}

+ (NSString *)pathAvatarImage:(NSString *)name {
    NSString *baseDir = [self baseDir];
    NSString *path = [NSString stringWithFormat:@"%@/Avatar/", baseDir];
    [HHPathTool createDirectory:path];
    if (name && name.length) {
        return [path stringByAppendingString:name];
    }
    return @"";
}

+ (NSString *)pathConv:(NSString *)convId {
    NSString *baseDir = [self baseDir];
    NSString *path = [NSString stringWithFormat:@"%@/%@/", baseDir, convId];
    return path;
}

+ (NSString *)pathConvHead:(NSString *)convId name:(NSString *)name {
    NSString *baseDir = [self baseDir];
    NSString *path = [NSString stringWithFormat:@"%@/%@/Head/", baseDir, convId];
    [HHPathTool createDirectory:path];
    if (name && name.length) {
        return [path stringByAppendingString:name];
    }
    return @"";
}

+ (NSString *)pathConvImage:(NSString *)convId name:(NSString *)name {
    NSString *baseDir = [self baseDir];
    NSString *path = [NSString stringWithFormat:@"%@/%@/Image/", baseDir, convId];
    [HHPathTool createDirectory:path];
    if (name && name.length) {
        return [path stringByAppendingString:name];
    }
    return @"";
}

+ (NSString *)pathConvVoice:(NSString *)convId name:(NSString *)name {
    NSString *baseDir = [self baseDir];
    NSString *path = [NSString stringWithFormat:@"%@/%@/Voice/", baseDir, convId];
    [HHPathTool createDirectory:path];
    if (name && name.length) {
        return [path stringByAppendingString:name];
    }
    return @"";
}

+ (NSString *)pathConvFile:(NSString *)convId name:(NSString *)name {
    NSString *baseDir = [self baseDir];
    NSString *path = [NSString stringWithFormat:@"%@/%@/File/", baseDir, convId];
    [HHPathTool createDirectory:path];
    if (name && name.length) {
        return [path stringByAppendingString:name];
    }
    return @"";
}

+ (NSString *)pathConvVideo:(NSString *)convId name:(NSString *)name {
    NSString *baseDir = [self baseDir];
    NSString *path = [NSString stringWithFormat:@"%@/%@/Video/", baseDir, convId];
    [HHPathTool createDirectory:path];
    if (name && name.length) {
        return [path stringByAppendingString:name];
    }
    return @"";
}

+ (void)removeConv:(NSString *)convId {
    NSString *path = [HHChatHelper pathConv:convId];
    [HHPathTool deleteFileAtPath:path];
}

@end
